require "Book"
require "Utils"


CommandBookstore = { }
CommandBookstore_mt = { __index = CommandBookstore }

function CommandBookstore.create(self, bookstore)
	local attributes = { bookstore = bookstore }
    setmetatable( attributes, CommandBookstore_mt )
    return attributes
end

function CommandBookstore._printBook(self, book)
	print(book.ISBN .. ':' .. book.author .. '-' .. book.title)
end
        

function CommandBookstore.getCommand( self )
    self:printHelp()

    while true do
        local commandLine = get_input('> ')
        local args = split(commandLine, ' ')
        if args ~= {} then
	        local command = args[1]
            local func = self[command]
            if func == nil then
	            print("unrecognized command 'help' for details")
	        else
	            if table.getn(args) > 1 then
	            	local jstr = ''
	            	for i = 2, table.getn(args) do
	            		jstr = jstr .. args[i]
	            	end
	                func( self, jstr )
	            else
	                func( self )
	            end
	        end
	    end
	end
	
end            

        
function CommandBookstore.printHelp(self)
    print('quit - finish')
    print('help - this help')
    print('list - list current books')
    print('save <filename> - save current booklist')
    print('load <filename> - load a booklist')
    print('delete <isbn> - delete a book')
    print('add - add a new book')
    print('find <author> - list books by author')
end            


function CommandBookstore.quit(self)
    os.exit()
end            

function CommandBookstore.help(self)
    self:printHelp()
end            

function CommandBookstore.list(self)
    local books = self.bookstore:getAllBooks()
    if table.getn(books) == 0 then
        print('bookstore empty')
        return
    end

    for key, book in ipairs(books) do
        self:_printBook(book)
    end
end            

function CommandBookstore.save(self, filename)
    self.bookstore:saveBooks(filename)
end            

function CommandBookstore.load(self, filename)
    self.bookstore:loadBooks(filename)
end            

function CommandBookstore.find(self, author)
    local books = self.bookstore:findBooksByAuthor( author )
    if table.getn(books) == 0 then
        print('no books by selected author')
        return
    end

    for key, book in ipairs(books) do
        self:_printBook(book)
    end
end            

function CommandBookstore.delete(self, isbn)
    book = self.bookstore:getBookByIsbn(isbn)
    if book == nil then
		print('Book not found')
        return
    end

    self.bookstore:removeBook( book.ISBN )
    print('book deleted')
end            

function CommandBookstore.add(self)
    local isbn = get_input('isbn: ')
    local title = get_input('title: ')
    local author = get_input('author: ')
    local description = get_input('description: ')
    
	local book = Book:create( isbn, title, description, author )
    if self.bookstore:addBook(book) then
        print('book added')
    else
        print('book not accepted')
    end
end            
